package my.myorm.tool.jdbc;

import my.mylocal.tool.MyLocal;

/**
 * SQLExecutor管理器，负责SQLExecutor的创建
 *
 * @author Skeven Huang
 * @version 1.0
 */
public class SQLManager {
    /**
     * 获取SQLExecutor，首先在当前事务中找，若找不到则创建
     *
     * @return
     * @throws Exception
     */

    public static SQLExecutor getSQLExecutor(String dsname) throws Exception {
        //MyLocal.put("datasource",dsname);
        SQLExecutor executor = TransactionContext.getSQLExecutorInCurrentTransaction();
        if (executor != null) return executor;
        return new SQLExecutor(dsname);
    }

    public static SQLExecutor getSQLExecutor() throws Exception {
        return getSQLExecutor("");
    }

    /**
     * 获取SQLExecutor，若withoutTransaction=true,则不考虑当前事务，直接创建，否则就在当前事务中提取
     *
     * @param withoutTransaction
     * @return
     * @throws Exception
     */
    public static SQLExecutor getSQLExecutor(boolean withoutTransaction) throws Exception {
        if (withoutTransaction) {
            return new SQLExecutor();
        }
        SQLExecutor executor = TransactionContext.getSQLExecutorInCurrentTransaction();
        return executor;
    }

    public static SQLExecutor getSQLExecutor(String dsname,boolean withoutTransaction) throws Exception {
        if (withoutTransaction) {
            return getSQLExecutor(dsname);
        }
        SQLExecutor executor = TransactionContext.getSQLExecutorInCurrentTransaction();
        return executor;
    }

    public static TDBExecutor getTDBExecutor(){
        return TDBExecutor.getInstance();
    }
}
